package com.ctgu.rabbitmq.rabbitmq.exchange.alternateExchange;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ ClassName AlternateExchangeConfig
 * @ Author Alex2
 * @ Date 2025/8/18 8:49
 **/
@Configuration
public class AlternateExchangeConfig {

    public static final String MAIN_EXCHANGE = "main_exchange";
    public static final String MAIN_QUEUE = "main_queue";

    public static final String ALTERNATE_EXCHANGE = "alternate_exchange";
    public static final String ALTERNATE_QUEUE = "alternate_queue";

    @Bean
    public DirectExchange mainExchange() {
        return ExchangeBuilder
                .directExchange(MAIN_EXCHANGE)      // 交换机名称
                .durable(true)
                .alternate(ALTERNATE_EXCHANGE)   // 设置备用交换机
                .build();
    }

    @Bean
    public FanoutExchange alternateExchange() {
        return new FanoutExchange(ALTERNATE_EXCHANGE);
    }

    @Bean
    public Queue mainQueue() {
        return new Queue(MAIN_QUEUE);
    }

    @Bean
    public Queue alternateQueue() {
        return new Queue(ALTERNATE_QUEUE);
    }

    @Bean
    public Binding bindingMain() {
        return BindingBuilder.bind(mainQueue()).to(mainExchange()).with("info");
    }

    @Bean
    public Binding bindingAlternate() {
        return BindingBuilder.bind(alternateQueue()).to(alternateExchange());
    }
}